package com.example.goodsmanagementcabinet.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.goodsmanagementcabinet.R;
import com.example.goodsmanagementcabinet.bean.KeyBean;
import com.example.goodsmanagementcabinet.otto.OttoManager;
import com.example.goodsmanagementcabinet.otto.OttoMsg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CheckKeyAdapter extends BaseAdapter {
    private Context mContext;
    private List mArrayList = new ArrayList<>();
    private Map<Integer, Boolean> map = new HashMap<>();
    private boolean isAllCheck = true;

    public CheckKeyAdapter(Context context) {
        this.mContext = context;
    }


    public void notifyRefresh(List list) {
        mArrayList.clear();
        mArrayList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return (mArrayList != null && mArrayList.size() == 0) ? 0 : mArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return mArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final HolderView holderView;
        if (view == null) {
            holderView = new HolderView();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_key, viewGroup, false);
            holderView.keyIv = view.findViewById(R.id.keyIv);
            holderView.tileLayout = view.findViewById(R.id.tileLayout);
            holderView.mRelativeR = view.findViewById(R.id.mRelative);
            holderView.mcheckBox = view.findViewById(R.id.mcheckBox);
            holderView.mcheckBox.setButtonDrawable(null);
            view.setTag(holderView);
        } else {
            holderView = (HolderView) view.getTag();
        }
        final KeyBean object = (KeyBean) mArrayList.get(i);
        switch (Integer.parseInt(object.getLocation())) {
            case 1:
                holderView.keyIv.setImageResource(R.mipmap.key_1);
                break;
            case 2:
                holderView.keyIv.setImageResource(R.mipmap.key_2);
                break;
            case 3:
                holderView.keyIv.setImageResource(R.mipmap.key_3);
                break;
            case 4:
                holderView.keyIv.setImageResource(R.mipmap.key_4);
                break;
            case 5:
                holderView.keyIv.setImageResource(R.mipmap.key_5);
                break;
            case 6:
                holderView.keyIv.setImageResource(R.mipmap.key_6);
                break;
            case 7:
                holderView.keyIv.setImageResource(R.mipmap.key_7);
                break;
            case 8:
                holderView.keyIv.setImageResource(R.mipmap.key_8);
                break;
            case 9:
                holderView.keyIv.setImageResource(R.mipmap.key_9);
                break;
            case 10:
                holderView.keyIv.setImageResource(R.mipmap.key_10);
                break;
            case 11:
                holderView.keyIv.setImageResource(R.mipmap.key_11);
                break;
            case 12:
                holderView.keyIv.setImageResource(R.mipmap.key_12);
                break;
            case 13:
                holderView.keyIv.setImageResource(R.mipmap.key_13);
                break;
            case 14:
                holderView.keyIv.setImageResource(R.mipmap.key_14);
                break;
            case 15:
                holderView.keyIv.setImageResource(R.mipmap.key_15);
                break;
            case 16:
                holderView.keyIv.setImageResource(R.mipmap.key_16);
                break;
            case 17:
                holderView.keyIv.setImageResource(R.mipmap.key_17);
                break;
            case 18:
                holderView.keyIv.setImageResource(R.mipmap.key_18);
                break;
            case 19:
                holderView.keyIv.setImageResource(R.mipmap.key_19);
                break;
            case 20:
                holderView.keyIv.setImageResource(R.mipmap.key_20);
                break;
            case 21:
                holderView.keyIv.setImageResource(R.mipmap.key_21);
                break;
            case 22:
                holderView.keyIv.setImageResource(R.mipmap.key_22);
                break;
            case 23:
                holderView.keyIv.setImageResource(R.mipmap.key_23);
                break;
            case 24:
                holderView.keyIv.setImageResource(R.mipmap.key_24);
                break;
            case 25:
                holderView.keyIv.setImageResource(R.mipmap.key_25);
                break;
            case 26:
                holderView.keyIv.setImageResource(R.mipmap.key_26);
                break;
            case 27:
                holderView.keyIv.setImageResource(R.mipmap.key_27);
                break;
            case 28:
                holderView.keyIv.setImageResource(R.mipmap.key_28);
                break;
            case 29:
                holderView.keyIv.setImageResource(R.mipmap.key_29);
                break;
            case 30:
                holderView.keyIv.setImageResource(R.mipmap.key_30);
                break;
            case 31:
                holderView.keyIv.setImageResource(R.mipmap.key_31);
                break;
            case 32:
                holderView.keyIv.setImageResource(R.mipmap.key_32);
                break;
            case 33:
                holderView.keyIv.setImageResource(R.mipmap.key_33);
                break;
        }
        if (object.getStatus().equals("1")) {
            holderView.tileLayout.setBackgroundColor(Color.parseColor("#53A73D"));
        } else if (object.getStatus().equals("2")) {
            holderView.tileLayout.setBackgroundColor(Color.parseColor("#DA8F1E"));
        } else {
            holderView.tileLayout.setBackgroundColor(Color.parseColor("#E6E2E2"));
        }
//        if (selectorPosition == i) {
//            holderView.tileLayout.setBackgroundColor(Color.parseColor("#BE2C38"));
//            OttoManager.getInstance().deliverMsg(OttoMsg.OTTO_CHECK_KEY, object);
//        } else {
//            if (object.getStatus().equals("1")) {
//                holderView.tileLayout.setBackgroundColor(Color.parseColor("#53A73D"));
//            } else if (object.getStatus().equals("2")) {
//                holderView.tileLayout.setBackgroundColor(Color.parseColor("#DA8F1E"));
//            } else {
//                holderView.tileLayout.setBackgroundColor(Color.parseColor("#E6E2E2"));
//            }
//        }
        holderView.mcheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked == true) {
                    map.put(i, true);
                    OttoManager.getInstance().deliverMsg(OttoMsg.OTTO_CHECK_KEY, object);
                    notifyDataSetChanged();
                } else {
                    map.remove(i);
                    OttoManager.getInstance().deliverMsg(OttoMsg.OTTO_REMOVE_CHECK_KEY, object);
                    notifyDataSetChanged();
                }
            }
        });
        if (map != null && map.containsKey(i)) {
            holderView.tileLayout.setBackgroundColor(Color.parseColor("#BE2C38"));
            holderView.mcheckBox.setChecked(true);
        } else {
            if (object.getStatus().equals("1")) {
                holderView.tileLayout.setBackgroundColor(Color.parseColor("#53A73D"));
            } else if (object.getStatus().equals("2")) {
                holderView.tileLayout.setBackgroundColor(Color.parseColor("#DA8F1E"));
            } else {
                holderView.tileLayout.setBackgroundColor(Color.parseColor("#E6E2E2"));
            }
            holderView.mcheckBox.setChecked(false);
        }
        return view;
    }

    public class HolderView {
        LinearLayout tileLayout;
        ImageView keyIv;
        CheckBox mcheckBox;
        RelativeLayout mRelativeR;
    }

    public void changeAllItem(boolean b) {
        isAllCheck = b;
        notifyDataSetChanged();
    }

}
